//
//  SubjectTableViewDataSource.m
//  LanguagesTeach
//
//  Created by Niel Santos on 2/9/13.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//

#import "SubjectDataSource.h"
#import "MetaSubject.h"
#import "RowTableCell.h"
#import "Entry.h"

@implementation SubjectDataSource
@synthesize subjectPath;


- (id)init: (SubjectPath*) localSubjectPath {
	
	NSString *fileDirectory;
	NSString *filePath;
	NSURL *url;
	NSXMLParser *xmlParser;
	BOOL success;
	
    if (self = [super init])
    {	
		subjectPath = localSubjectPath;
		fileDirectory = [NSString stringWithFormat:@"data/%@/%@/%@/%@", 
						 [localSubjectPath languagePrefix],
						 [localSubjectPath levelFolder],
						 [localSubjectPath unitSubFolder],
						 [localSubjectPath subject]];
		filePath = [[NSBundle mainBundle] pathForResource:@"subject" ofType:@"xml" inDirectory: fileDirectory];
		url = [NSURL fileURLWithPath:filePath];
		
		
		xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
		xmlSubjectParser = [[XMLSubjectParser alloc] init];
		xmlParser.delegate = xmlSubjectParser;
		success = [xmlParser parse];
		[xmlParser release];
		
		if(success){
			NSLog(@"No errors found");
		}else{
			NSLog(@"Warning a error was found");
		}
    }
    return self;	
}


- (NSInteger)numberOfRowsInSection {
	NSMutableArray *entries;
	entries = [xmlSubjectParser entries];
	return [entries count];
	
}


// Customize the appearance of table view cells for exercises
- (FigureDescriptionCell*)cellForRowAtIndexes:(NSInteger) imageIndex descriptionIndex: (NSInteger) descriptionIndex soundIndex: (NSInteger) soundIndex{
	NSMutableArray *entries;
	Entry *imageEntry;
	Entry *descriptionEntry;
	Entry *soundEntry;
	
	FigureDescriptionCell *cell;
	
    cell = [[[FigureDescriptionCell alloc] init] autorelease];
	
	
	entries = [xmlSubjectParser entries];
	imageEntry = [entries objectAtIndex: imageIndex];
	descriptionEntry = [entries objectAtIndex: descriptionIndex];
	soundEntry = [entries objectAtIndex: soundIndex];
	
	
	//Image
	cell.figureImage = imageEntry;
	
	//Description
	cell.figureDescription = [entry imageDescription];
	
	//Audio
	[cell figureSound] = ;
	

	
	return cell;
}

// Customize the appearance of table view cells for introduction
- (FigureDescriptionCell*)cellForRowAtIndexPath:(NSInteger)indexFigure {
	return [self cellForRowAtIndexes:indexFigure descriptionIndex:indexFigure soundIndex: indexFigure];
}


- (void)dealloc {
	[xmlSubjectParser release];
    [super dealloc];
}

@end